from pymongo import MongoClient


def save_to_mongodb(news_list):
    client = MongoClient('mongodb://localhost:27017/')
    db = client['news_db']
    collection = db['hot_news']

    # 删除旧数据
    collection.delete_many({})

    # 插入新数据
    if news_list:
        collection.insert_many([dict(n) for n in news_list])
        print(f"已存储{len(news_list)}条数据到MongoDB")